package com.editso.haier.controller.user;
import com.editso.haier.entity.User;
import com.editso.haier.result.Result;
import com.editso.haier.service.ShoppingService;
import com.editso.haier.util.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;


@RestController
@RequestMapping("/shopping")
@RequiresUser
public class ShoppingController {

    private ShoppingService shoppingService;

    @Autowired
    public void setShoppingService(ShoppingService shoppingService) {
        this.shoppingService = shoppingService;
    }

    @GetMapping
    public Result getMyShoppingCar(){
        Object principal = SecurityUtils.getSubject().getPrincipal();
        System.out.println(principal);
        return shoppingService.getShopping(principal.toString());
    }

    @PostMapping
    public Result addCommodityShopping(@RequestBody Map<String, String> map){
        Object principal = SecurityUtils.getSubject().getPrincipal();
        return shoppingService.addShopping(map.get("cid"), 1, principal.toString());
    }

    @PutMapping
    public Result alterCommodityShopping(@RequestBody Map<String, Object> map){
        return shoppingService.alterShopping(
                map.get("shoppingId").toString(),
                Integer.valueOf(Utils.get(map.get("count").toString(), "0"))
        );
    }

    @DeleteMapping
    public Result deleteCommodityShopping(String shoppingId){
        return shoppingService.deleteShopping(shoppingId);
    }

}
